package com.synjones.handsetS8.ild.net.api;

import com.google.gson.Gson;
import com.synjones.handsetS8.ild.constants.Headers;
import com.synjones.handsetS8.ild.constants.HttpDefault;
import com.synjones.handsetS8.ild.net.common.HttpUtils;
import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetClient;
import com.synjones.handsetS8.ild.net.model.LaborResult;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.net.model.TeamResult;
import com.synjones.handsetS8.ild.net.model.WorkTypeRedundantResult;
import com.synjones.handsetS8.ild.net.model.WorkerRegParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;

/**
 * 用工登记 接口
 */
public class WorkerRegApi {
    private static final String BASE_URL = "http://jiangong-manager.dev1.i-laodong.com/";
    private static IdCardService service = NetClient.getRetrofit(BASE_URL).create(IdCardService.class);

    /**
     * 获取分包公司列表
     * @param callback
     */
    public static void getLaborList(NetCallback<NetResult<List<LaborResult>>> callback) {
        Call<NetResult<List<LaborResult>>> call = service.getLaborList(HttpUtils.getAuthKey());
        call.enqueue(callback);
    }

    /**
     * 获取班组列表
     * @param laborId
     * @param callback
     */
    public static void getTeamList(long laborId, NetCallback<NetResult<List<TeamResult>>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("labor_id", laborId);

        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(map));
        Call<NetResult<List<TeamResult>>> call = service.getTeamList(HttpUtils.getAuthKey(), requestBody);
        call.enqueue(callback);
    }

    /**
     * 获取工种列表
     * @param callback
     */
    public static void getWorkTypeList(NetCallback<NetResult<WorkTypeRedundantResult>> callback) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", "work_type");

        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(map));
        Call<NetResult<WorkTypeRedundantResult>> call = service.getWorkTypeList(requestBody);
        call.enqueue(callback);
    }

    /**
     * 用工登记
     * @param param
     * @param callback
     */
    public static void reg(WorkerRegParam param, NetCallback<NetResult> callback) {
        System.out.println(new Gson().toJson(param));
        RequestBody requestBody = RequestBody.create(MediaType.parse(HttpDefault.CONTENT_TYPE_JSON), new Gson().toJson(param));
        Call<NetResult> call = service.reg(HttpUtils.getAuthKey(), requestBody);
        call.enqueue(callback);
    }

    interface IdCardService {
        /**
         * 获取分包单位列表
         *
         * @return
         */
        @POST("api/project-labor/list")
        Call<NetResult<List<LaborResult>>> getLaborList(@Header(Headers.AUTH_KEY) String authKey);

        /**
         * 获取班组列表
         *
         * @return
         */
        @POST("api/project-labor/search-team")
        Call<NetResult<List<TeamResult>>> getTeamList(@Header(Headers.AUTH_KEY) String authKey,
                                                      @Body RequestBody body);

        /**
         * 获取工种列表
         *
         * @return
         */
        @POST("api/data/dictionary")
        Call<NetResult<WorkTypeRedundantResult>> getWorkTypeList(@Body RequestBody body);

        /**
         * 员工登记
         *
         * @param body
         * @return
         */
        @POST("api/worker/create")
        Call<NetResult> reg(@Header(Headers.AUTH_KEY) String authKey, @Body RequestBody body);
    }
}
